// SMDB.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MovieDatabase.h"
#include <algorithm>

bool SortActorByMovieBilling(const Actor& left, const Actor& right)
{
   return left.CreditsBillingPosition() < right.CreditsBillingPosition();
}

int _tmain(int argc, _TCHAR* argv[])
{
   string file1 = "actresses.list";//"TestFile.txt";
   string file2 = "actors.list";
   string searchString = "Angelina Jolie";
   MovieDatabase database;
   database.ImportIMDBFile(file1);
   auto actorMatches = database.SearchActors(searchString, true);
   if (actorMatches.size() > 0)
   {
      cout << "Found " << actorMatches.size() << "possible match" << (actorMatches.size() > 1 ? "es" :"") << endl;
      for (int ixPerson = 0; ixPerson < actorMatches.size(); ixPerson++)
      {
         cout << ixPerson +1 << ") " << actorMatches[ixPerson].FullName() << endl;
         auto bodyOfWork = database.GetBodyOfWork(actorMatches[ixPerson]);
         size_t maxDisplayMovies = min(15u, bodyOfWork.size());
         for(int ixMovie = 0; ixMovie < maxDisplayMovies; ixMovie++)
         {
            cout << bodyOfWork[ixMovie].Title << endl;
            cout << "\tCast members:" << endl;
            auto movieCast = database.Cast(bodyOfWork[ixMovie]);
            sort(movieCast.begin(), movieCast.end(), SortActorByMovieBilling);
            size_t maxCastMembers = min(15u, movieCast.size());
            for (int ixCastMember = 0; ixCastMember < maxCastMembers; ixCastMember++)
            {
               if (movieCast[ixCastMember] == actorMatches[ixPerson])
               {
                  cout << "\t\t{This actor}";
               }
               else
               {
                  cout << "\t\tActor Name: " << movieCast[ixCastMember].FullName();
               }
               if (movieCast[ixCastMember].HasValidCharacterName())
                  cout << " Character: " << movieCast[ixCastMember].CharacterName();
               cout << endl;
            }
            if (maxCastMembers < movieCast.size())
               cout << movieCast.size() - maxCastMembers << " more cast members..." << endl;
         }
         if (maxDisplayMovies < bodyOfWork.size())
            cout << bodyOfWork.size() - maxDisplayMovies << " more movies..." << endl;

      }
   }
   else
   {
      cout << "No match for actor \"" << searchString << "\"" << endl;
   }

   //system("pause");
   return 0;
}

